package com.base.common.permission;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.provider.Settings;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;

import com.base.common.R;
import com.orhanobut.logger.Logger;
import com.tbruyelle.rxpermissions3.RxPermissions;

import io.reactivex.rxjava3.disposables.Disposable;


/**
 * @Date: 2018/12/20. 16:30
 * @Author: hong_world
 * @Description: 使用RxPermissions 统一进行申请权限，申请结果：通过/被拒绝/被拒绝后不再询问；可自行定义申请结果的默认操作
 * @Version:
 */
public class PermissionManager {
    public static Disposable checkPermission(Fragment context, OnPermissionListener listener, String... permissions) {
        return checkPermissionImp(new RxPermissions(context), listener, permissions);
    }

    public static Disposable checkPermission(FragmentActivity context, OnPermissionListener listener, String... permissions) {
        return checkPermissionImp(new RxPermissions(context), listener, permissions);
    }

    /**
     * 申请权限 未定义任何默认操作
     *
     * @param rxPermissions
     * @param listener
     * @param permissions
     * @return
     */
    private static Disposable checkPermissionImp(RxPermissions rxPermissions, OnPermissionListener listener, String... permissions) {
        return rxPermissions.requestEachCombined(permissions)
                .subscribe(permission -> {
                    if (permission.granted) {
                        Logger.i("PermissionManager 用户同意了该权限");
                        if (listener != null)
                            listener.passPermission();
                    } else if (permission.shouldShowRequestPermissionRationale) {
                        Logger.i("PermissionManager 用户拒绝了该权限，没有选中『不再询问』，可进行弹窗说明，确认后重新请求权限");
                        if (listener != null)
                            listener.showRequestPermissionRationale();
                    } else {
                        Logger.i("PermissionManager 用户拒绝了该权限，选中『不再询问』，可进行二次弹窗，确认后进入app设置页面");
                        if (listener != null)
                            listener.deniedPermission();
                    }
                }, throwable -> {
                    if (listener != null)
                        listener.onError(throwable);
                    Logger.e("PermissionManager " + throwable.getMessage());
                });
    }

    public static void askForPermission(Activity mActivity, String msg) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mActivity).setIcon(R.mipmap.ic_launcher);
        builder.setTitle("手动设置权限");
        builder.setMessage("如需正常使用该功能需要授予：" + msg + "权限" + "\n设置流程：应用信息—>权限中进行设置");
        builder.setNegativeButton("取消", (dialog, which) -> {

        });
        builder.setPositiveButton("去设置", (dialog, which) -> {
            Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
            intent.setData(Uri.parse("package:" + mActivity.getPackageName())); // 根据包名打开对应的设置界面
            mActivity.startActivity(intent);
        });
        builder.create().show();
    }

}